package reflect;

import java.lang.reflect.Field;

/**
 * 反射机制的滥用对String的破坏
 */
public class ReflectDemo9 {
    public static void main(String[] args) throws Exception {
        String s1 = "abc";
        System.out.println("s1:"+s1);

        Class cls = String.class;
        Field field = cls.getDeclaredField("value");
        field.setAccessible(true);
//        char[] value = (char[]) field.get(s1);
//        for(char c :value){
//            System.out.println(c);
//        }
        //利用暴力反射,将s1对象的内容进行修改
        field.set(s1,new char[]{'h','e','l','l','o'});

        System.out.println("s1:"+s1);//s1:hello

        String s2 = "abc";
        System.out.println("s2:"+s2);//s2:hello

        String s3 = "abc";
        System.out.println("s3:"+s3);//s3:hello
    }
}








